## MessageCenter 单元测试
## 使用 GUT (Godot Unit Test) 框架
## https://github.com/bitwes/Gut

extends GutTest

var message_center: Node


func before_each():
	# 创建MessageCenter实例
	message_center = load("res://MessageCenter.gd").new()
	add_child(message_center)


func after_each():
	# 清理
	if message_center:
		message_center.clear_all()
		message_center.queue_free()


## 测试基础发送和订阅
func test_basic_send_and_subscribe():
	var received = false
	var received_data = null
	
	message_center.subscribe(self, "test_channel", func(sender, data):
		received = true
		received_data = data
	)
	
	message_center.send(self, "test_channel", "test_data")
	
	assert_true(received, "应该接收到消息")
	assert_eq(received_data, "test_data", "数据应该匹配")


## 测试多订阅者
func test_multiple_subscribers():
	var count = 0
	
	var sub1 = Node.new()
	var sub2 = Node.new()
	var sub3 = Node.new()
	
	message_center.subscribe(sub1, "channel", func(s, d): count += 1)
	message_center.subscribe(sub2, "channel", func(s, d): count += 1)
	message_center.subscribe(sub3, "channel", func(s, d): count += 1)
	
	message_center.send(self, "channel", null)
	
	assert_eq(count, 3, "三个订阅者都应该接收到消息")
	
	sub1.free()
	sub2.free()
	sub3.free()


## 测试取消订阅
func test_unsubscribe():
	var received = false
	
	message_center.subscribe(self, "channel", func(s, d):
		received = true
	)
	
	message_center.send(self, "channel", null)
	assert_true(received, "取消订阅前应该接收到消息")
	
	received = false
	message_center.unsubscribe(self, "channel")
	message_center.send(self, "channel", null)
	
	assert_false(received, "取消订阅后不应该接收到消息")


## 测试单参数回调
func test_single_parameter_callback():
	var received_sender = null
	
	message_center.subscribe(self, "channel", func(sender):
		received_sender = sender
	, 1)
	
	var test_sender = Node.new()
	message_center.send(test_sender, "channel", "ignored_data")
	
	assert_eq(received_sender, test_sender, "应该接收到发送者")
	test_sender.free()


## 测试双参数回调
func test_two_parameters_callback():
	var received_sender = null
	var received_data = null
	
	message_center.subscribe(self, "channel", func(sender, data):
		received_sender = sender
		received_data = data
	, 2)
	
	var test_sender = Node.new()
	message_center.send(test_sender, "channel", "test_data")
	
	assert_eq(received_sender, test_sender, "应该接收到发送者")
	assert_eq(received_data, "test_data", "应该接收到数据")
	test_sender.free()


## 测试匿名订阅
func test_anonymous_subscribe():
	var received = false
	
	var subscriber = message_center.subscribe_anonymous("channel", func(s, d):
		received = true
	)
	
	assert_not_null(subscriber, "应该返回订阅者对象")
	
	message_center.send(self, "channel", null)
	assert_true(received, "匿名订阅者应该接收到消息")
	
	received = false
	message_center.unsubscribe(subscriber, "channel")
	message_center.send(self, "channel", null)
	assert_false(received, "取消后不应该接收到消息")


## 测试订阅数量统计
func test_subscription_count():
	assert_eq(message_center.get_subscription_count("channel"), 0, "初始应该为0")
	
	message_center.subscribe(self, "channel", func(s, d): pass)
	assert_eq(message_center.get_subscription_count("channel"), 1, "订阅后应该为1")
	
	var sub2 = Node.new()
	message_center.subscribe(sub2, "channel", func(s, d): pass)
	assert_eq(message_center.get_subscription_count("channel"), 2, "再次订阅后应该为2")
	
	message_center.unsubscribe(self, "channel")
	assert_eq(message_center.get_subscription_count("channel"), 1, "取消订阅后应该为1")
	
	sub2.free()


## 测试空发送者（应该报错）
func test_null_sender():
	message_center.subscribe(self, "channel", func(s, d): pass)
	
	# 应该在控制台输出错误，但不会崩溃
	message_center.send(null, "channel", "data")
	
	# 测试通过（没有崩溃）
	assert_true(true)


## 测试空频道名（应该报错）
func test_empty_channel():
	# 应该在控制台输出错误
	message_center.subscribe(self, "", func(s, d): pass)
	message_center.send(self, "", "data")
	
	# 测试通过（没有崩溃）
	assert_true(true)


## 测试取消所有订阅
func test_unsubscribe_all():
	message_center.subscribe(self, "channel1", func(s, d): pass)
	message_center.subscribe(self, "channel2", func(s, d): pass)
	message_center.subscribe(self, "channel3", func(s, d): pass)
	
	assert_eq(message_center.get_subscription_count("channel1"), 1)
	assert_eq(message_center.get_subscription_count("channel2"), 1)
	assert_eq(message_center.get_subscription_count("channel3"), 1)
	
	message_center.unsubscribe_all(self)
	
	assert_eq(message_center.get_subscription_count("channel1"), 0)
	assert_eq(message_center.get_subscription_count("channel2"), 0)
	assert_eq(message_center.get_subscription_count("channel3"), 0)


## 测试清空所有订阅
func test_clear_all():
	message_center.subscribe(self, "channel1", func(s, d): pass)
	message_center.subscribe(self, "channel2", func(s, d): pass)
	
	var sub = Node.new()
	message_center.subscribe(sub, "channel3", func(s, d): pass)
	
	message_center.clear_all()
	
	assert_eq(message_center.get_all_channels().size(), 0, "所有频道应该被清空")
	
	sub.free()


## 测试在回调中发送消息（嵌套发送）
func test_nested_send():
	var inner_received = false
	
	message_center.subscribe(self, "inner_channel", func(s, d):
		inner_received = true
	)
	
	message_center.subscribe(self, "outer_channel", func(sender, data):
		message_center.send(sender, "inner_channel", "nested")
	)
	
	message_center.send(self, "outer_channel", "trigger")
	
	assert_true(inner_received, "嵌套发送应该正常工作")


## 测试在回调中取消订阅
func test_unsubscribe_in_callback():
	var count = 0
	var subscriber = Node.new()
	
	message_center.subscribe(subscriber, "channel", func(s, d):
		count += 1
		message_center.unsubscribe(subscriber, "channel")
	)
	
	message_center.send(self, "channel", null)
	assert_eq(count, 1, "第一次应该接收到")
	
	message_center.send(self, "channel", null)
	assert_eq(count, 1, "取消订阅后不应该再接收")
	
	subscriber.free()
